﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.StatementManage;
using SmartWMS.Server.Repository;
using SmartWMS.Server.StatementServer.ForewarningServices.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.StatementServer.ForewarningServices
{
    public class ForewarningService : IForewarningService
    {
        private readonly IRepository<Product> _productRepository;
        private readonly IRepository<Warehouse> _warehouseRepository;
       
        public ForewarningService(IRepository<Product> productRepository, IRepository<Warehouse> warehouseRepository)
        {
            _productRepository = productRepository;
            _warehouseRepository = warehouseRepository;
        }

        /// <summary>
        /// 库存预警列表(分页)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<List<ForewarningDto>>> GetForewarningDto(ForewarningSelect dto)
        {
            var product=  _productRepository.GetListQuery();
            var warehouse = _warehouseRepository.GetListQuery();
            var list=(from a in product
                     join b in warehouse on a.DefaultWarehouseId equals b.Id
                        select new ForewarningDto
                        {
                            Id = a.Id,
                            ProductCode = a.ProductCode,
                            ProductName = a.ProductName,
                            Specs = a.Specs,
                            Ceiling = a.Ceiling,
                            Floor = a.Floor,
                            WarehouseName = b.Name,
                            Inventory = a.Inventory
                        }).ToList();
            if(!string.IsNullOrEmpty(dto.code))
            {
                list = list.Where(x => x.ProductCode.Contains(dto.code)).ToList();
            }
            if (!string.IsNullOrEmpty(dto.pName))
            {
                list = list.Where(x => x.ProductName.Contains(dto.pName)).ToList();
            }
            if (!string.IsNullOrEmpty(dto.wName))
            {
                list = list.Where(x => x.WarehouseName==dto.wName).ToList();
            }
            int totalCount= list.Count;
            int pageCount=(int)Math.Ceiling((double)totalCount / dto.pageSize);
            list= list.Skip((dto.pageIndex - 1) * dto.pageSize).Take(dto.pageSize).ToList();
            return Response<List<ForewarningDto>>.Ok(list, totalCount, pageCount);
        }

        /// <summary>
        /// 仓库列表
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Warehouse>>> GetWarehouseList()
        {
            var warehouseList = await _warehouseRepository.GetAllListAsync();
            var response = warehouseList.MapToList<Warehouse>();
            return response != null ? Response<List<Warehouse>>.Ok(response) : Response<List<Warehouse>>.Fail("查询失败");
        }
    }
}
